package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/driver/desktop"
	"image"
	"image/color"
	"image/jpeg"
	"image/png"
	"os"
)

var _ widgetcommon.IWidgetItem = (*ImageButton)(nil)
var _ widgetcommon.IUserInfo = (*ImageButton)(nil)

func init() {
	widget.RegWidget("WidgetButton", "图片按钮1", NewImageButton)

	widget.RegWidget("ButtonEmpty", "图片按钮2", NewImageButton)
}

type ImageButton struct {
	*WidgetContainer
	//Button *widget2.Button
	image *canvas.Image
	//三态图片资源
	//normalImage  *canvas.Image
	//hoverImage   *canvas.Image
	//pressedImage *canvas.Image

	normalImage1  *image.Image
	hoverImage1   *image.Image
	pressedImage1 *image.Image

	normalFile  string
	hoverFile   string
	pressedFile string

	*Action
	*ButtonEvent
	rootNode widgetcommon.IWidgetItem //父节点
	name     string

	hover   bool
	pressed bool
}

func NewImageButton(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}

	button := &ImageButton{
		image: &canvas.Image{FillMode: canvas.ImageFillOriginal},
		//normalImage:  &canvas.Image{FillMode: canvas.ImageFillOriginal},
		//hoverImage:   &canvas.Image{FillMode: canvas.ImageFillOriginal},
		//pressedImage: &canvas.Image{FillMode: canvas.ImageFillOriginal},
		//Button:      widget2.NewButton("", func() {}),
		Action:      &Action{},
		ButtonEvent: &ButtonEvent{},
		WidgetContainer: &WidgetContainer{
			Container: &Container{
				name: name,
			},
			Border: &canvas.Rectangle{
				FillColor:   color.Transparent,
				StrokeColor: color.RGBA{255, 0, 0, 255}, //黄色
				StrokeWidth: 2,
			},
		},
		rootNode: rootNode,
	}
	button.WidgetContainer.ac = button.Action
	button.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), button)
	//button.WidgetContainer.SetObjects([]fyne.CanvasObject{button.image})
	button.WidgetContainer.SetObjectsInterface([]fyne.CanvasObject{button.image})
	button.WidgetContainer.SetName(name)
	button.SetBase(button, rootNode)
	button.ExtendBaseWidget(button)
	button.name = name
	button.OnTapped = imageButtonCallBack(name, button)
	return button
}

func imageButtonCallBack(name string, item widgetcommon.IWidgetItem) func() {
	return func() {
		cb := widget.GetButtonCallBack(name)
		if cb == nil || item == nil {
			return
		}
		cb(item, item.GetUserInfo())
	}
}
func (b *ImageButton) SetProperty(key, value string, isProperty bool) {
	b.WidgetContainer.SetProperty(key, value, isProperty)
	if (key == "PressTexture" && value == "AutoPress") ||
		(key == "PressTexture" && value == "ActionPress") {
		return
	}
	if key == "NormalTexture" {
		b.SetNormalImagePath(value)
	} else if key == "PressTexture" {
		b.SetPressImagePath(value)
	} else if key == "HoverTexture" {
		b.SetHoverImagePath(value)
	}
}
func (b *ImageButton) SetNormalImagePath(path string) {
	b.normalFile = path
	b.normalImage1 = b.loadImagBase(path)

	//b.normalImage.FillMode = canvas.ImageFillOriginal
	//b.normalImage = b.loadImag(path)
	//b.normalImage.File = path // 设置图片路径
	//b.normalImage.FillMode = canvas.ImageFillOriginal
	//b.normalImage.Refresh() // 刷新控件以显示新图片
	if b.image.File == "" {
		//b.image = b.normalImage
		//b.WidgetContainer.SetObjectsInterface([]fyne.CanvasObject{b.image})
		b.image.Image = *b.normalImage1
		b.image.Refresh()
	}
	//b.image.File = path // 设置图片路径
	//b.image.Refresh()   // 刷新控件以显示新图片
}
func (b *ImageButton) SetPressImagePath(path string) {
	b.pressedFile = path
	b.pressedImage1 = b.loadImagBase(path)

	//b.pressedImage.FillMode = canvas.ImageFillOriginal
	//b.pressedImage = b.loadImag(path)
	//b.pressedImage.File = path
	//b.pressedImage.FillMode = canvas.ImageFillOriginal
	//b.pressedImage.Refresh()
}

func (b *ImageButton) loadImagBase(path string) *image.Image {
	// 打开图像文件
	imgFile, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer imgFile.Close()

	// 解码图像
	var img image.Image
	if ext := getFileExtension(path); ext == ".png" {
		img, err = png.Decode(imgFile)
	} else if ext == ".jpg" || ext == ".jpeg" {
		img, err = jpeg.Decode(imgFile)
	} else {
		panic("unsupported image format")
	}

	if err != nil {
		panic(err)
	}

	return &img
}
func (b *ImageButton) loadImag(path string) *canvas.Image {
	// 打开图像文件
	imgFile, err := os.Open(path)
	if err != nil {
		panic(err)
	}
	defer imgFile.Close()

	// 解码图像
	var img image.Image
	if ext := getFileExtension(path); ext == ".png" {
		img, err = png.Decode(imgFile)
	} else if ext == ".jpg" || ext == ".jpeg" {
		img, err = jpeg.Decode(imgFile)
	} else {
		panic("unsupported image format")
	}

	if err != nil {
		panic(err)
	}

	canvasImage := canvas.NewImageFromImage(img)
	_min := img.Bounds().Min
	_max := img.Bounds().Max
	var _size = fyne.NewSize(float32(_max.X-_min.X), float32(_max.Y-_min.Y))
	canvasImage.Resize(_size)
	return canvasImage
}

// 获取文件扩展名
func getFileExtension(filename string) string {
	if len(filename) < 4 {
		return ""
	}
	return filename[len(filename)-4:]
}
func (b *ImageButton) SetHoverImagePath(path string) {
	b.hoverFile = path
	b.hoverImage1 = b.loadImagBase(path)

	//b.hoverImage.FillMode = canvas.ImageFillOriginal
	//b.hoverImage = b.loadImag(path)
	//b.hoverImage.File = path
	//b.hoverImage.FillMode = canvas.ImageFillOriginal
	//b.hoverImage.Refresh()
}
func (i *ImageButton) MinSize() fyne.Size {
	//return fyne.NewSize(100, 100)
	return i.image.MinSize()
}
func (b *ImageButton) updateImage() {
	//var lastSize = b.image.Size()
	if b.hoverFile == "" &&
		b.pressedFile == "" {
		return
	}
	if b.pressed {
		b.image.Image = *b.pressedImage1
		//b.image = b.pressedImage
	} else if b.hover {
		b.image.Image = *b.hoverImage1
		//b.image = b.hoverImage
	} else {
		b.image.Image = *b.normalImage1
		//b.image = b.normalImage
	}

	//b.image.Resize(lastSize)
	//b.WidgetContainer.SetObjectsInterface([]fyne.CanvasObject{b.image})
	b.image.Refresh()
	b.Refresh() // 刷新以更新显示
}
func (b *ImageButton) MouseIn(ev *desktop.MouseEvent) {
	if widgetcommon.IsEditMode {
		b.Action.MouseIn(ev)
		return
	}
	b.hover = true
	b.updateImage()
}

func (b *ImageButton) MouseOut() {
	if widgetcommon.IsEditMode {
		b.Action.MouseOut()
		return
	}
	b.hover = false
	b.updateImage()
}

func (b *ImageButton) MouseDown(ev *desktop.MouseEvent) {
	if widgetcommon.IsEditMode {
		b.Action.MouseDown(ev)
		return
	}
	b.pressed = true
	b.updateImage()
}

func (b *ImageButton) MouseUp(ev *desktop.MouseEvent) {
	if widgetcommon.IsEditMode {
		b.Action.MouseUp(ev)
		return
	}
	b.pressed = false
	b.updateImage()
}

func (b *ImageButton) ForEachProperty(cb func(k, v string)) {
	b.IUserInfo.ForEachProperty(func(key, value string) {
		cb(key, value)
	})
}

/*
func (b *ImageButton) CreateRenderer() fyne.WidgetRenderer {
	return &imageButtonRenderer{
		button:  b,
		Border:  b.Border,
		objects: []fyne.CanvasObject{b.Border, b.image},
	}
}

type imageButtonRenderer struct {
	button *ImageButton

	Border  *canvas.Rectangle
	objects []fyne.CanvasObject
}

func (r *imageButtonRenderer) Layout(size fyne.Size) {
	r.button.image.Resize(size)
}

func (r *imageButtonRenderer) MinSize() fyne.Size {
	return r.button.image.MinSize()
}

func (r *imageButtonRenderer) Refresh() {
	//r.button.image.Refresh()
	for _, obj := range r.objects {
		obj.Refresh()
	}
}

func (r *imageButtonRenderer) Objects() []fyne.CanvasObject {
	//return []fyne.CanvasObject{r.button.image, r.button.Border}
	return r.objects
}

func (r *imageButtonRenderer) Destroy() {}
*/
